var client = require("../DAOImpl/mongodb");
var dataConfig = require("../config/database.json");
var host = dataConfig.host;
var database = dataConfig.db;
var db = new client(host, database);
var status = require("../config/status.json");
var notification = require('../private/notification');

function sleep(milliSecond) {
	var startTime = new Date().getTime();
	console.log(startTime);
	while (new Date().getTime() <= milliSecond + startTime) {
	}
	console.log(new Date().getTime());
}
/*
 * @req.body cus_lat cus_lng cus_id {"cus_lat":
 * 31.202818,"cus_lng":121.557706,"cus_id":1}
 */
function bookNow(req, res, next) {
	console.log('bookNow');
	console.log(req.body.cus_lng + '  ' + req.body.cus_lat);
	var result = new Object();
	var selector = new Object();
	var info = new Obejct();
	info.cus_id = req.body.cus_id;
	info.status = status.ORDERSTATUS.CUSTOMERREQUEST.VALUE;

	db.insert(dataConfig.col_order, info, function(err1, err2, doc_order) {
		if (err1 || err2) {
			result.status = status.SYSTEMERROR.VALUE;
			res.send(result);
		} else {

			// send notification to the driver
			db.find(dataConfig.col_driver, {}, function(err3, err4, doc) {
				if (err3 || err4) {
					console.log(err3 + err4);
				} else {
					var message = new Object();
					message.cus_id = req.body.cus_id;
					message.cus_lat = req.body.cus_lat;
					message.cus_lag = req.body.cus_lag;
					var notifi = new notification(doc, message, function(err,
							req, res, data) {
						if (err) {
							console.log(err);
						} else {
							console.log('notification is done!');
							result.status = status.SUCCESS.VALUE;
							result.order_id = doc_order._id;
							res.send(result);
						}
					});
				}
			});
		}
	});
	return next();
}

/*
 * @req.body: cus_id {'cus_id': '1'} type , counts [1,2,3]
 */

function showOrder(req, res, next) {
	var result = new Object();
	//if showmyorder, type = 0; if showhisorder, type = 1
	var type = req.query.type;
	if(type == 0){
		var query1 ;
		query1.cus_id = req.query.cus_id;
		query1.status = status.ORDERSTATUS.WAITING.VALUE;
		db.find(dataConfig.col_order, query1, function(err1, err2, doc_waiting){
			if(err1 || err2){
				result.status = status.SYSTEMERROR.VALUE;
				res.send(result);
			}else{
				var query2 ;
				query2.cus_id = req.query.cus_id;
				query2.status = status.ORDERSTATUS.SUCCESS.VALUE;
				db.find(dataConfig.col_order, query2, function(err3, err4, doc_success){
					if(err3 || err4){
						result.status = status.SYSTEMERROR.VALUE;
						res.send(result);
					}else{
						result.order_waiting = doc_waiting,
						result.order_success = doc_success;
						result.status = status.SUCCESS.VALUE;
						res.send(result);
					}
				});
			}
		});
	} else if (type == 1) {
		var query1;
		query.cus_id = req.query.cus_id;
		db.find(dataConfig.col_order, query1 ,function(err1, err2, docs) {
			if (err3 || err4) {
					result.status = status.SYSTEMERROR.VALUE;
					res.send(result);
				} else {
					result.order_history = docs;
					var count = 0;
					for(var index in docs){
						if(docs[index].status === status.ORDERSTATUS.SUCCESS.VALUE){
							count ++;
						}
					}
					result.succ_percent = count / docs.length;
					result.status = status.SUCCESS.VALUE;
					res.send(result);
				}
			});
	}
	
	return next();
}

/*
 * @req.body: cus_id {'cus_id': '1'}
 */

function showHisOrder(req, res, next) {
	res.redirect("/myorder");
	return next();
}

// /*
// * @req.body: cus_id
// * {'cus_id': '1'}
// */
//
// function showOrder(req, res, next){
// var result =
// {
// 'resultCode': 0,
// 'dri_info':
// {
// 'name': 'admin',
// 'taxi_number':'',
// 'phone':'',
// 'credit':'',
// 'photo':''
// },
// 'order':
// {
// 'order_id':'',
// 'type':1,// 1 book now
// // 2 book taxi
// 'status':1 ,// 1 success
// // 2 waiting
// 'time':'',
// 'start':'',
// 'end':''
// },
// 'succ_num':'',
// 'waiting_num':'',
// };
// res.send(result);
// return next();
// }
//
// /*
// * @req.body: cus_id
// * {'cus_id': '1'}
// */
// function showHisOrder(req, res, next){
// return next;
// }

/*
 * cancel the order if the order is created. @path /cancelorder @req.body:
 * cus_id {'order_id': '1'}
 */

function cancelOrder(req, res, next) {
	var result = new Object();
	var selector = new Object();
	var document = new Object();

	selector.cus_id = req.body.cus_id;
	selector.stauts = {
		$ne : status.ORDERSTATUS.CUSTOMERACCEPT.VALUE
	};
	document.status = status.ORDERSTATUS.CANCEL.VALUE;

	db.find(dataConfig.col_driver, {}, {
		fields : {
			_id : 1
		}
	}, function(err1, err2, doc) {
		if (err1 || err2) {
			console.log(err1 + err2);
			result.status = status.SYSTEMERROR.VALUE;
			res.send(result);
		} else {
			var message = new Object();
			var notifi = new notification(doc, message, function(err, req, res,
					data) {
				if (err) {
					console.log(err);
					result.status = status.SYSTEMERROR.VALUE;
					result.err = err;
					res.send(result);
				} else {
					console.log('notification is done!');
					db.update(dadaConfig.col_order, selector, document,
							function(err3, err4, doc) {
								if (err3 || err4) {
									result.status = status.SYSTEMERROR.VALUE;
									res.send(result);
								} else {
									result.status = status.SUCCESS.VALUE;
									res.send(result);
								}
							});
				}
			});
		}
	});
	return next;
}
exports.services = [ bookNow, showOrder, showHisOrder, cancelOrder ];